var e = require("../../@babel/runtime/helpers/interopRequireDefault"), t = e(require("../../@babel/runtime/regenerator")), a = require("../../@babel/runtime/helpers/asyncToGenerator"), n = e(require("../../behavior/indexBehavior")), r = require("../../utils/tool"), o = getApp(), s = require("../../utils/api.js"), i = (require("../../utils/util.js"), 
require("../../http/productApi.js")), u = require("../../http/userApi.js"), c = require("../../http/activityApi.js");

Page({
    onShowCounter: 0,
    behaviors: [ n.default ],
    isOnFg: !1,
    shareFromType: 0,
    shareFromUserID: 0,
    productId: 0,
    queueId: "",
    queueIdOld: "",
    openLogined: !1,
    timer: null,
    data: {
        inited: !1,
        systemInfo: o.data.systemInfo,
        swiperCurrentIndex: 0,
        swiperAnimationDuration: 500,
        showDetailDialog: !1,
        showShareDialog: !1,
        title: null,
        detailImageList: null,
        shareInfo: null
    },
    onLoad: function(e) {
        var n = this;
        return a(t.default.mark(function a() {
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    n.shareFromType = e.fromType, n.shareFromUserID = e.fromUserID, n.productId = e.productId, 
                    e.queueId && (n.queueId = e.queueId);

                  case 4:
                  case "end":
                    return t.stop();
                }
            }, a);
        }))();
    },
    onShow: function() {
        var e = this;
        return a(t.default.mark(function a() {
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    e.isOnFg = !0, o.globalData.callback = function(t) {
                        "BRIGHT_CLAASSIFY_CHAGER" == t.businessType && (console.log("队伍有变化"), e.loadData(!1, !1, !0, !1, !0)), 
                        "BRIGHT_OPENBOX_CHAGER" == t.businessType && (console.log("盒堆有变化"), e.loadData(!1, !1, !0, !1, !1)), 
                        "BRIGHT_CLAASSIFY_TO_ME" == t.businessType && (console.log("排到我了"), e.loadData(!1, !1, !0, !1, !0));
                    }, e.onShowCounter++, 1 == e.onShowCounter ? e.loadData(!1, !0, !0, !0, !0) : o.isLogin() ? e.data.inited ? e.loadData(!1, !1, !0, !1, !0) : e.loadData(!1, !0, !0, !0, !0) : o.returnUI();

                  case 4:
                  case "end":
                    return t.stop();
                }
            }, a);
        }))();
    },
    onHide: function() {
        this.isOnFg = !1;
        var e = this.selectComponent("#cd");
        e && e.stopTimer();
    },
    onUnload: function() {
        this.isOnFg = !1, this.cancelJoin();
        var e = this.selectComponent("#cd");
        e && e.stopTimer();
    },
    loadTopRank: function(e) {
        var n = this;
        return a(t.default.mark(function a() {
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    c.rankProductIndex({
                        productNo: e
                    }).then(function(e) {
                        n.setData({
                            rankInfo: e.data
                        });
                    }).catch(function(e) {});

                  case 1:
                  case "end":
                    return t.stop();
                }
            }, a);
        }))();
    },
    cancelJoin: function() {
        var e = this;
        return a(t.default.mark(function a() {
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    if (console.log(e.data.myIndex), 0 != e.data.myIndex) {
                        t.next = 4;
                        break;
                    }
                    return t.next = 4, i.boxLineExit();

                  case 4:
                  case "end":
                    return t.stop();
                }
            }, a);
        }))();
    },
    onShareAppMessage: function(e) {
        return {
            title: this.data.staticInfo.brightBoxAttr.shareTile,
            imageUrl: this.data.staticInfo.brightBoxAttr.shareImg,
            path: "/pages/series/series?productId=" + this.productId + "&queueId=" + (this.queueId || "") + "&fromType=0&shareFrom=" + o.getUsername() + "&fromUserID=" + o.getUsername()
        };
    },
    swiperOnChange: function(e) {
        var n = this;
        return a(t.default.mark(function a() {
            var r, s;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    if (console.log(111), r = e.detail.current, s = e.detail.source, 1 != r || "touch" != s) {
                        t.next = 10;
                        break;
                    }
                    return n.queueId = "", n.setData({
                        inited: !1
                    }), o.showLoading(), t.next = 9, n.cancelJoin();

                  case 9:
                    n.loadData(!0, !1, !0, !0, !0);

                  case 10:
                  case "end":
                    return t.stop();
                }
            }, a);
        }))();
    },
    swipeShowBox: function() {
        this.setData({
            swiperAnimationDuration: 0
        }), this.setData({
            swiperCurrentIndex: 0
        }), this.setData({
            swiperAnimationDuration: 500
        });
    },
    productDetailOnTap: function() {
        this.setData({
            showDetailDialog: !0
        });
    },
    boxItemOnSelected: function(e) {
        if (console.log("【选中小盒】", e), this.data.myIndex < 0) o.showToast("请先排队"); else if (this.data.myIndex > 0) o.showToast("还没轮到你，再等一会儿吧~"); else {
            var t = e.detail.index, a = this.data.dynamicInfo.boxList[t].boxId, n = this;
            o.data.boxStaticInfo = this.data.staticInfo, wx.navigateTo({
                url: "/pages/open/open?productId=" + this.productId + "&queueId=" + this.queueId + "&boxId=" + a,
                events: {
                    cdOnEnd: function() {
                        console.log("【抽盒页】开盒页倒计时结束返回"), n.setData({
                            myCd: null
                        }), n.joinLineOnTap();
                    }
                }
            });
        }
    },
    closeDetailDialog: function() {
        this.setData({
            showDetailDialog: !1
        });
    },
    shareRuleOnTap: function() {
        this.loadShareInfo(!0);
    },
    loadShareInfo: function(e) {
        var n = this;
        return a(t.default.mark(function e() {
            var a;
            return t.default.wrap(function(e) {
                for (;;) switch (e.prev = e.next) {
                  case 0:
                    return e.prev = 0, e.next = 3, u.shareInfo();

                  case 3:
                    a = e.sent, n.setData({
                        showShareDialog: !0,
                        shareInfo: {
                            rule: "分享链接后被不同的玩家点击每次获得5心愿值，如对方为新玩家，额外赠送20心愿值/个",
                            todayCurrent: a.data.shareBrightProductCount,
                            todayTotal: a.data.taskNum
                        }
                    }), e.next = 9;
                    break;

                  case 7:
                    e.prev = 7, e.t0 = e.catch(0);

                  case 9:
                  case "end":
                    return e.stop();
                }
            }, e, null, [ [ 0, 7 ] ]);
        }))();
    },
    closeShareDialog: function() {
        this.setData({
            showShareDialog: !1
        });
    },
    returnToLine: function() {
        this.productId = this.data.otherLineInfo.productId, this.queueId = this.data.otherLineInfo.queueId, 
        this.loadData(!1, !0, !0, !1, !0);
    },
    joinLineOnTap: function(e) {
        var n = this;
        return a(t.default.mark(function a() {
            var o;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    if (wx.showLoading({
                        title: "加载中…"
                    }), t.prev = 1, e) {
                        t.next = 5;
                        break;
                    }
                    return t.next = 5, s.delay(new Date().getTime(), 1e3, {});

                  case 5:
                    return t.next = 7, i.boxLineAdd(n.productId, n.queueId);

                  case 7:
                    return o = t.sent, console.log(o), "排队排满了" == o.msg && (0, r.toastToPage)(o.msg), 
                    t.next = 12, n.loadData(!1, !1, !0, !1, !0);

                  case 12:
                    e && n.data.myIndex > 0 && ("dALgALHLRMCUBaslyGHw7MxZv6CQasb6bSTPZWOWcu4", wx.requestSubscribeMessage({
                        tmplIds: [ "dALgALHLRMCUBaslyGHw7MxZv6CQasb6bSTPZWOWcu4" ]
                    })), t.next = 18;
                    break;

                  case 15:
                    t.prev = 15, t.t0 = t.catch(1), console.log(t.t0);

                  case 18:
                    return t.prev = 18, wx.hideLoading(), t.finish(18);

                  case 21:
                  case "end":
                    return t.stop();
                }
            }, a, null, [ [ 1, 15, 18, 21 ] ]);
        }))();
    },
    wishListOntap: function() {
        wx.navigateTo({
            url: "/packages/wish/page/wish-list/wish-list"
        });
    },
    tryOnTap: function() {
        wx.navigateTo({
            url: "/pages/try-series/try-series"
        });
    },
    cdOnEnd: function() {
        var e = this;
        this.isOnFg && wx.showModal({
            title: "重新排队提示",
            content: "等待时间过长，已被移出队列，是否继续排队？",
            success: function(t) {
                t.confirm ? e.joinLineOnTap() : t.cancel && o.returnUI();
            }
        });
    },
    loadData: function(e, n, r, s, i) {
        var u = this;
        return a(t.default.mark(function a() {
            var c;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    if (o.showLoading(), t.prev = 1, u.loadMyInfo(), !n) {
                        t.next = 8;
                        break;
                    }
                    return t.next = 6, u.loadStaticInfo();

                  case 6:
                    t.next = 9;
                    break;

                  case 8:
                  case 9:
                    if (!r) {
                        t.next = 14;
                        break;
                    }
                    return t.next = 12, u.loadDynamicInfo();

                  case 12:
                    t.next = 15;
                    break;

                  case 14:
                  case 15:
                    if (!s) {
                        t.next = 20;
                        break;
                    }
                    return t.next = 18, u.autoJoinLine();

                  case 18:
                    t.next = 21;
                    break;

                  case 20:
                  case 21:
                    return t.next = 23, u.autoExitLine();

                  case 23:
                    if (!i) {
                        t.next = 28;
                        break;
                    }
                    return t.next = 26, u.loadLineList();

                  case 26:
                    t.next = 29;
                    break;

                  case 28:
                  case 29:
                    return t.next = 31, u.loadMyLineInfo();

                  case 31:
                    u.setData({
                        inited: !0
                    }), (c = u.selectComponent("#cd")) && c.init(), e && u.swipeShowBox(), t.next = 40;
                    break;

                  case 37:
                    t.prev = 37, t.t0 = t.catch(1), console.log(t.t0);

                  case 40:
                    return t.prev = 40, wx.hideLoading(), t.finish(40);

                  case 43:
                  case "end":
                    return t.stop();
                }
            }, a, null, [ [ 1, 37, 40, 43 ] ]);
        }))();
    },
    loadStaticInfo: function() {
        var e = this;
        return a(t.default.mark(function a() {
            var n;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.next = 2, i.boxDetail(e.productId, e.shareFromUserID, e.shareFromType);

                  case 2:
                    (n = t.sent).data.productDetails = n.data.productDetails.replace(/\<img/gi, '<img class="block"'), 
                    n.data.productDetails = n.data.productDetails.replace(/\<p/gi, '<p class="break"'), 
                    e.setData({
                        staticInfo: n.data
                    }), e.loadTopRank(n.data.productNo);

                  case 7:
                  case "end":
                    return t.stop();
                }
            }, a);
        }))();
    },
    loadDynamicInfo: function() {
        var e = this;
        return a(t.default.mark(function a() {
            var n;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.next = 2, i.boxQueue(e.productId, e.queueId, e.queueIdOld);

                  case 2:
                    n = t.sent, e.queueId = n.data.queueId, e.queueIdOld = e.queueId, e.setData({
                        dynamicInfo: n.data
                    });

                  case 6:
                  case "end":
                    return t.stop();
                }
            }, a);
        }))();
    },
    loadLineList: function() {
        var e = this;
        return a(t.default.mark(function a() {
            var n;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    if (2 != e.data.dynamicInfo.salesStatus) {
                        t.next = 3;
                        break;
                    }
                    return e.setData({
                        lineList: []
                    }), t.abrupt("return");

                  case 3:
                    return t.next = 5, i.boxLineList(e.productId, e.queueId);

                  case 5:
                    n = t.sent, e.setData({
                        lineList: n.data || []
                    });

                  case 7:
                  case "end":
                    return t.stop();
                }
            }, a);
        }))();
    },
    loadMyInfo: function() {
        var e = this;
        return a(t.default.mark(function a() {
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.next = 2, o.downloadUserInfo();

                  case 2:
                    e.setData({
                        myInfo: {
                            wishValue: o.data.loginInfo.userInfo.availableWish
                        }
                    });

                  case 3:
                  case "end":
                    return t.stop();
                }
            }, a);
        }))();
    },
    loadMyLineInfo: function() {
        var e = this;
        return a(t.default.mark(function a() {
            var n, r, o, s;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.next = 2, i.myLineInfo();

                  case 2:
                    n = t.sent, r = -1, o = -1, s = null, n.data ? n.data.productId == e.productId && n.data.memberQueueId == e.queueId ? (1 == n.data.index ? (r = 0, 
                    o = parseInt((n.data.overDate - n.data.thisDate) / 1e3)) : (r = n.data.index - 1, 
                    o = -1), s = null) : (r = -1, o = -1, s = {
                        productId: n.data.productId,
                        queueId: n.data.memberQueueId,
                        index: n.data.index
                    }) : (r = -1, o = -1, s = null), e.setData({
                        myIndex: r,
                        myCd: o,
                        otherLineInfo: s
                    });

                  case 8:
                  case "end":
                    return t.stop();
                }
            }, a);
        }))();
    },
    autoJoinLine: function() {
        var e = this;
        return a(t.default.mark(function a() {
            var n;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    if (2 != e.data.dynamicInfo.salesStatus) {
                        t.next = 2;
                        break;
                    }
                    return t.abrupt("return");

                  case 2:
                    if (1 != e.data.dynamicInfo.salesStatus) {
                        t.next = 4;
                        break;
                    }
                    return t.abrupt("return");

                  case 4:
                    return t.next = 6, i.myLineInfo();

                  case 6:
                    if (!((n = t.sent).data && n.data.index > 1)) {
                        t.next = 9;
                        break;
                    }
                    return t.abrupt("return");

                  case 9:
                    if (!(e.data.dynamicInfo.queueClassifyMember > 0)) {
                        t.next = 11;
                        break;
                    }
                    return t.abrupt("return");

                  case 11:
                    return t.next = 13, i.boxLineAdd(e.productId, e.queueId);

                  case 13:
                  case "end":
                    return t.stop();
                }
            }, a);
        }))();
    },
    autoExitLine: function() {
        var e = this;
        return a(t.default.mark(function a() {
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    if (0 == e.data.dynamicInfo.salesStatus) {
                        t.next = 4;
                        break;
                    }
                    if (e.data.otherLineInfo || !(e.data.myIndex >= 0)) {
                        t.next = 4;
                        break;
                    }
                    return t.next = 4, i.boxLineExit();

                  case 4:
                  case "end":
                    return t.stop();
                }
            }, a);
        }))();
    }
});